<template>
  <div class="level">
    <h1>医院</h1>
    <div class="content">
      <div class="left">等级:</div>
      <ul class="hospital">
        <!-- 如果 activeFlag 为 '',就有 active 这个类 -->
        <li :class="{active:activeFlag==''}">全部</li>
        <!-- 如果 activeFlag 为 item.value , 就有 active 类 -->
        <li 
        v-for="item in levelArr" 
        :key="item.value"
        @click="changeLevel(item.value)"
        :class="{active:activeFlag == item.value}"
        >{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reqHospitalLevelAndRegion } from "@/api/home";
import { onMounted, ref } from "vue";
import { HospitalLevelAndRegionResponseData,HospitalLevelAndRegionArr } from '@/api/home/type'
// 定义一个数组 存储医院等级数据
let levelArr = ref<HospitalLevelAndRegionArr>([])
// 定义一个 控制高亮 的响应式数据
let activeFlag = ref<string>('')
onMounted(() => {
  getLevel()
})
// 获取医院等级数据
const getLevel = async() => {
  const res:HospitalLevelAndRegionResponseData = await reqHospitalLevelAndRegion("HosType")
  // console.log(res)
  if (res.code == 200) {
    levelArr.value = res.data
  }
}
// 点击等级小 li触发
const changeLevel = (level:string) => {
  // 把 等级 给 高亮 flag
  activeFlag.value = level
  // 传给 父组件 level
  $emit('getLevel',level)
}
let $emit = defineEmits(['getLevel'])
</script>
<!-- 给组件取一个名字 方便在 vue扩展好找到 -->
<script lang="ts">
export default {
  name: "Level",
}
</script>

<style scoped lang="scss">
.level {
  color: #7f7f7f;
  margin-top: 20px;
  h1 {
    font-size: 900;
    margin: 10px 0;
  }
  .content {
    display: flex;
    .left {
      margin-right: 10px;
    }
    .hospital {
      display: flex;
      li{
        margin-right: 10px;
        &.active {
          color:  #55a6fe;
        }
      }
      li:hover {
        color: #55a6fe;
        cursor: pointer;
      }
    }
  }
}
</style>